#include "HeatPhysics.h"

#include "Time.h"

// Constructor
HeatPhysics::HeatPhysics(void)
{
	 minTemp = -10.0f;
	 maxTemp = 10.0f;
	 curTemp = 0.0f;
	 startTemp = 0.0f;
	 coolRate = 0.0f;
	 appliedHeat = false;
}

// Destructor
HeatPhysics::~HeatPhysics(void)
{
}

void HeatPhysics::SetMaxTemp(float temp)
{
	this->maxTemp = temp;
}
void HeatPhysics::SetMinTemp(float temp)
{
	this->minTemp = temp;
}
void HeatPhysics::SetCurTemp(float temp)
{
	this->curTemp = temp;
}
void HeatPhysics::SetCoolRate(float rate)
{
	this->coolRate = rate;
}

void HeatPhysics::Update(float deltaTime)
{
	if(this->appliedHeat == false)
		this->CoolDown(deltaTime);
	appliedHeat = false;
}

// ApplyHeat
//	- Applies the specified amount of heat to the object.
//	- The heat is constrained between minTemp & maxTemp.
void HeatPhysics::ApplyHeat(float amount)
{
	curTemp += amount * Time::GetInstance().DeltaTime();

	if(curTemp > maxTemp)
		curTemp = maxTemp;
	if(curTemp < minTemp)
		curTemp = minTemp;

	appliedHeat = true;
}

// CoolDown
//	- Updates the object's temperature over time to return
//	  it to 0, based on the object's coolRate.
void HeatPhysics::CoolDown(float deltaTime)
{
	if(curTemp > 0.0f)
	{
		curTemp -= coolRate*deltaTime;
		if(curTemp < 0.0f)
			curTemp = 0.0f;
	}
	else if( curTemp < 0.0f)
	{
		curTemp += coolRate*deltaTime;
		if(curTemp > 0.0f)
			curTemp = 0.0f;
	}
}


D3DCOLOR HeatPhysics::GetColorFromTemp(void) const
{
	if(curTemp > startTemp)
	{
		//Determine how red the object will be drawn
		float tempPercent = curTemp / maxTemp;
		int redColor = (int)(255 *tempPercent);
		return D3DCOLOR_ARGB(125,redColor,0,0);
	}
	else if(curTemp < startTemp)
	{
		//Determine how blue the object will be drawn
		float tempPercent = fabs( curTemp / minTemp);
		int blueColor = (int)(255*tempPercent);
		return D3DCOLOR_ARGB(125,0,0,blueColor);
	}
	else
		return D3DCOLOR_ARGB(1,0,0,0);
	
}